<template>
    <!--登录模块-->
    <div class="login-div" v-show="ifShow">
      <!--左侧显示登录图片-->
      <img class="f1" src="../assets/img/logo_4.png" />
      <!--右侧显示登录信息-->
      <div class="fr popup-box-1">
        <section class="popup-box-1-1 f1">
          <!--使用v-on指令实现单击 按钮，隐藏登录模块-->
          <span class="popup-box-close fr" @click="closeLogin()">
  </span>
        </section>
        <h4 class="login-title">账 号 登 录</h4>
        <section class="popup-box-2 popup-margin">
          <input type="text" placeholder="请输入账号" class=" tc-input active" v-model="username" />
          <section class="login-code">
            <input type="password" placeholder="请输入密码" class="f1" v-model="password" />
          </section>
          <!--使用v-on指令实现单击“登录”按钮，登录网站-->
          <section class="popup-box-btn a-mouse" @click="login">登 录</section>
        </section>
      </div>
    </div>
</template>
<script setup>
import {ref} from 'vue'
import axios from 'axios'					//导入Axios
import {useStore} from 'vuex'
//获取HeaderNav组件（顶部导航模块）传递的数据
const props=defineProps(['ifShow'])
const emit=defineEmits(['closeLogin'])	//声明自定义事件
const username=ref('')
const password=ref('')
const store=useStore()
//定义隐藏登录模块的方法
const closeLogin=()=>{
    username.value=''
    password.value=''
    //使用emit()方法触发自定义事件closeLogin
    emit('closeLogin')
}
//定义登录网站的方法
const login=()=>{
    var getUser={username:username.value,password:password.value}
    //使用Axios发送post请求
    axios.post('http://localhost:3001/login',getUser,{timeout: 3000}).then((res)=>{
        //flag的值为1时，表示登录成功
    if(res.data.flag===1){
        //使用commit()方法触发Store实例的mutations属性中的方法
        store.commit('login',{
        username:username.value,
        password:password.value
        })
        alert(res.data.msg)			//显示服务器返回的登录成功信息
        emit('closeLogin')			//触发自定义事件，隐藏登录模块
        username.value=''
        password.value=''
    }else{
        //flag的值不为1时，表示登录失败，显示登录失败信息
        alert(res.data.msg)
    }
    })
}
</script>
  
<style scoped>
/* 登录模块样式 */
.login-div {
    width: 900px;
    height: 515px;
    overflow: hidden;
    background-color: #FFFFFF;
    border-radius: 8px;
    border: 1px solid grey;
    z-index: 50000;
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translateX(-50%) translateY(-50%);
    -ms-transform: translateX(-50%) translateY(-50%);
    -moz-transform: translateX(-50%) translateY(-50%);
    -webkit-transform: translateX(-50%) translateY(-50%);
    margin-left: auto;
    margin-right: auto;
}

/* 登录图片位置 */
.f1 {
    float: left;
}

/* 登录图片样式 */
img {
    border: none;
    vertical-align: middle;
    overflow: hidden;
    image-rendering: -moz-crisp-edges;
    image-rendering: -o-crisp-edges;
    image-rendering: -webkit-optimize-contrast;
    image-rendering: crisp-edges;
}

/* 右侧登录样式 */
.fr {
    float: right;
}

.popup-box-1 {
    padding: 20px 20px 0px;
    width: calc(100% - 460px);
    height: 330px;
    overflow: hidden;
    position: relative;
    margin-top: 10px;
}

.popup-box-1-1 {
    width: 100%;
    height: 32px;
    line-height: 32px;
    overflow: hidden;
    font-size: 14px;
}

.popup-box-close {
    cursor: pointer;
    width: 16px;
    height: 16px;
    background: url(../assets/img/nav_39.png) no-repeat;
}

.popup-box-1 h4 {
    font-size: 30px;
}

h4 {
    margin: 0px;
    padding: 0px;
    list-style: none;
    outline: none;
    word-wrap: break-word;
}

.popup-box-2 {
    width: calc(100% - 100px);
    padding: 0px 60px;
    height: auto;
    overflow: hidden;
}

.popup-margin {
    margin-top: 50px;
}

.tc-input {
    width: calc(100% - 10px);
    padding: 5px;
    border: none;
    outline: none;
    line-height: 24px;
    height: 36px;
    font-size: 14px;
    margin-top: 10px;
}

.active {
    border-bottom: 1px solid #E7E8EC;
}

.login-code {
    width: calc(100% - 10px);
    padding: 5px;
    height: 36px;
    border-bottom: 1px solid #E7E8EC;
    overflow: hidden;
    margin-top: 20px;
}

.login-code input {
    width: calc(100% - 90px);
    line-height: 36px;
    height: 36px;
    border: none;
    outline: none;
    font-size: 14px;
}

.popup-box-btn {
    user-select: none;
    width: 100%;
    height: 40px;
    line-height: 40px;
    background-color: #16CA82;
    color: #ffffff;
    border-radius: 20px;
    font-size: 16px;
    font-weight: bold;
    box-shadow: 0px 6px 6px 0px rgba(34, 204, 173, 0.13);
    margin: 20px auto 10px;
}

.a-mouse {
    cursor: pointer;
}
</style>